//
//  WebViewController.m
//  BaseTdkc
//
//  Created by DavidHuang on 16/7/1.
//  Copyright © 2016年 黄栋炜. All rights reserved.
//

#import "WebViewController.h"

@interface WebViewController ()<UIWebViewDelegate,UIScrollViewDelegate>
@property (strong, nonatomic) UIWebView *webview;
@property (strong, nonatomic) UIActivityIndicatorView *activity;


@end

@implementation WebViewController{
    NSString *_title;
    NSString *_urlStr;
}

- (instancetype)initWithUrlStr:(NSString *)urlStr title:(NSString *)title {
    if (self == [super init]) {
        self.title = title;
        _urlStr = urlStr;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:_urlStr]];
    [self.webview loadRequest:request];
    [NSURLConnection connectionWithRequest:request delegate:self];
    //    [self.webview mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.edges.equalTo(self.view);
    //    }];
    //    [self.activity mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.size.mas_equalTo(CGSizeMake(30, 30));
    //        make.center.equalTo(self.view);
    //    }];
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    [self.activity setHidden:NO];
    [self.activity startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    if (self.activity.hidden == NO) {
        [self.activity setHidden:YES];
        [self.activity stopAnimating];
    }
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    if (self.activity.hidden == NO) {
        [self.activity setHidden:YES];
        [self.activity stopAnimating];
    }
}

- (UIWebView *)webview {
    if(_webview == nil) {
        _webview = [[UIWebView alloc] init];
        _webview.delegate = self;
        [self.view addSubview:_webview];
    }
    return _webview;
}

- (UIActivityIndicatorView *)activity {
    if(_activity == nil) {
        _activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [self.view addSubview:_activity];
    }
    return _activity;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
